<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Order_detail extends CI_Controller {

	public $data;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Order_detail_model');
	}

	public function index($id)
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		if(null == $id)
			redirect('order');			

		$cond = array(
			'order_id' => $id
		);
		//get data from company table
		$order_details = $this->Order_detail_model->get_all($cond);

		$userdata = $this->session->userdata('userInfo');

		$data = array(
			'title' => 'Khai nghiệp | Báo giá',
			'name' => $userdata['name'],
			'id' => $userdata['id'],
			'order_id' => $id,
 			'order_details' => $order_details
		);

		$this->load->view('tmpl/header', $data);
		$this->load->view('tmpl/left', $data);
		$this->load->view('order_detail_view', $data);
		$this->load->view('tmpl/footer');
	}

	public function add_new()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
	
		//get data
		$order_id = $_POST['order_id'];
		$prod_id = $_POST['prod_id'];
		$count = $_POST['count'];
		$virtual_price = $_POST['virtual_price'];
		$real_total = $_POST['real_total'];
		$virtual_total = $_POST['virtual_total'];

		if((null != $order_id) && (null != $prod_id) && (null != $count) && (null != $virtual_price) && (null != $real_total) && (null != $virtual_total)) {//if data not null
			
			$new_data = array(
				'order_id' => $order_id,
				'prod_id' => $prod_id,
				'count' => $count,
				'virtual_price' => $virtual_price,
				'real_total' => $real_total,
				'virtual_total' => $virtual_total
			);

			$id = $result = $this->Order_detail_model->add_new($new_data); //id = -1: fail, else 
			//success
			if($id != -1) { //insert success
				$rp = array('id' => $id);
				echo json_encode($rp);
			}else
				return false;
		}else
			return false;
	}

	//edit
	public function edit()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$order_id = $_POST['order_id'];
		$prod_id = $_POST['prod_id'];
		$count = $_POST['count'];
		$virtual_price = $_POST['virtual_price'];
		$real_total = $_POST['real_total'];
		$virtual_total = $_POST['virtual_total'];

		if((null != $order_id) && (null != $prod_id) && (null != $count) && (null != $virtual_price) && (null != $real_total) && (null != $virtual_total)) {//if data not null
			
			$cond = array(
				'order_id' => $order_id,
				'prod_id' => $prod_id
			);

			$new_data = array(
				'prod_id' => $prod_id,
				'count' => $count,
				'virtual_price' => $virtual_price,
				'real_total' => $real_total,
				'virtual_total' => $virtual_total
			);
			
			//success
			if($this->Order_detail_model->edit($new_data, $cond)) { //update success
				return true;
			}else
				return false;
		}else
			return false;
	}

	//delete
	public function delete()
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		if(null != $id) {
			$cond = array(
				'id' => $id
			);
			
			if($this->Order_model->delete($cond)) { //delete success
				return true;
			}else
				return false;

		}else 
			return false;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */